import java.io.*;
import java.net.*;

public class EventHandler
{
	DataOutputStream outStream;
	VoIPGui theGui;

	public EventHandler(Socket outSocket, VoIPGui theGui) throws Exception
	{
		outStream = new DataOutputStream(outSocket.getOutputStream());
		this.theGui = theGui;
	}//constructor

	public void HandleEvent(VoIPEvent event) throws Exception
	{
		String eventType = event.GetType();

		if(0 == eventType.compareTo("AcquireResource")) {
			HandleAcquireResource(event);
		} 
		else if (0 == eventType.compareTo("LampOn")) {
			theGui.SetLampStatus(true);
		}
		else if (0 == eventType.compareTo("LampOff")) {
			theGui.SetLampStatus(false);
		}
		else if (0 == eventType.compareTo("PlayTone")) {
			theGui.JLabel1.setText("Playing tone " + event.GetEntry("Tone") + " with cadence " + event.GetEntry("Cadence"));
		}
		else if (0 == eventType.compareTo("DisplayString")) {
			HandleDisplayString(event);
		}
		else if (0 == eventType.compareTo("AppendString")) {
			HandleAppendString(event);
		}
		else if (0 == eventType.compareTo("HandsetOn")) {
			theGui.SetHookswitchStatus(true);
		}
		else if (0 == eventType.compareTo("HandsetOff")) {
			theGui.SetHookswitchStatus(false);
		}
		
		else {
			System.out.println("Other event: " + eventType);
		}//if
		
		
	}//HandleEvent


	public void HandleAcquireResource(VoIPEvent event) throws Exception
	{
		String resource = event.GetEntry("Resource");

		System.out.println("Got acquire resource event: " + resource);

		String Message = "<ResourceAcquired><Function/><Resource>" + resource + "</Resource></ResourceAcquired>";
		outStream.writeBytes(Message + "\n\n");
	}//HandleAcquireResource

	public void HandleDisplayString(VoIPEvent event) throws Exception
	{
		String string = event.GetEntry("String");
		Integer lineNum = new Integer(event.GetEntry("lineNum"));
		Integer linePos = new Integer(event.GetEntry("linePos"));
		
		theGui.UpdateDisplay(string, lineNum, linePos);
	}
	
	public void HandleAppendString(VoIPEvent event) throws Exception
	{
		String string = event.GetEntry("String");
		
		theGui.AppendDisplay(string);
	}
	

}


